from pages.basePage1 import BasePage
import time
import allure
from pages.image_template import ImageTemplate
from config.setting import host_port

# 定位层
class HomePage(BasePage):
    def __init__(self):
        BasePage.__init__(self)
        self.driver.get(f'http://{host_port}/SharedParkingPlace/tenant/index.html')
        time.sleep(1)

    # 定位地图上的停车场
    def elementParking(self):
        return self.driver.find_element_by_xpath('//div[@style="position: absolute; height: 0px; width: 0px; left: 0px; top: 0px; z-index: 700;"]//span[3]')

    # 定位按钮 去这里
    def elementGoHere(self):
        return self.driver.find_element_by_xpath('//h4[@id="myModalLabel"]//button')

    # 定位搜索框 搜索地址
    def elementSearch(self):
        return self.driver.find_element_by_xpath('//input[@id="suggestId"]')

    # 定位按钮 查看详情
    def elementDetails(self):
        return self.driver.find_element_by_xpath('//div[@class="modal-body"]//tr[2]//td[5]//button[2]')

    # 定位按钮 查看详情-关闭
    def elementClose(self):
        return self.driver.find_element_by_xpath('//div[@class="col-xs-9 col-sm-12 col-md-12 text-right"]//button')

    # 定位按钮 抢购
    def elementPurchase(self):
        return self.driver.find_element_by_xpath('//div[@class="modal-body"]//tr[2]//td[5]//button[1]')

    # 定位按钮 下单
    def elementOrder(self):
        return self.driver.find_element_by_xpath('//button[@class="btn btn-primary"]')

    # 定位下单成功后的提示框
    def elementOrderSucceed(self):
        return self.driver.find_element_by_xpath('//div[@id="buyresult"]//h3')

# 操作层
class HomeHandle(HomePage):
    def __init__(self):
        HomePage.__init__(self)

    # 切换表单
    @allure.step('抢租客-主页-切换表单')
    def switchFrame(self):
        self.driver.switch_to.frame('testIframe')

    # 点击地图上的停车场小红点
    @allure.step('抢租客-主页-点击地图上的停车场小红点')
    def clickParking(self):
        element = self.elementParking()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击 去这里
    @allure.step('抢租客-主页-点击【去这里】')
    def clickGoHere(self):
        element = self.elementGoHere()
        self.driver.execute_script('arguments[0].click();', element)

    # 切换窗口
    @allure.step('抢租客-主页-切换到百度地图窗口')
    def switchWindow(self, window):
        '''
        :param window: 指定切换到all_handles中的哪一个窗口
        :return:
        '''
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[window])

    # 获取text
    @allure.step('抢租客-主页-获取百度地图窗口的text')
    def getText(self):
        time.sleep(1)
        return self.driver.title

    # 在搜索框搜索地址框中输入地址，点击第一个地址
    @allure.step('在搜索框搜索地址框中输入地址，点击第一个地址')
    def inputSearch(self, address):
        element = self.elementSearch()
        element.send_keys(address)
        elementSearch = self.driver.find_element_by_xpath('//div[@class="tangram-suggestion"]//tr[1]/td')
        self.driver.execute_script('arguments[0].click();', elementSearch)

    # 点击查看详情
    @allure.step('抢租客-主页-点击【查看详情】')
    def clickDetails(self):
        element = self.elementDetails()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击按钮 查看详情-【关闭】
    @allure.step('抢租客-主页-点击【查看详情】-点击关闭')
    def clickClose(self):
        element = self.elementClose()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击按钮 抢购
    @allure.step('抢购')
    def clickPurchase(self):
        element = self.elementPurchase()
        self.driver.execute_script('arguments[0].click();', element)

    # 点击按钮 下单
    @allure.step('下单')
    def clickOrder(self):
        element = self.elementOrder()
        self.driver.execute_script('arguments[0].click();', element)

    # 获取下单成功后提示框的内容
    @allure.step('下单成功后提示框')
    def getOrderText(self):
        element = self.elementOrderSucceed().text
        return element

# 业务层
class HomeBusiness(HomeHandle):
    def __init__(self):
        HomeHandle.__init__(self)
        # 实例化图像识别模块
        self.imageObj = ImageTemplate()

    # 抢租客-主页-点击小红点停车位-点击去这里
    def goHere(self):
        # 切换表单
        self.switchFrame()
        # 点击停车位小红点
        self.clickParking()
        time.sleep(1)
        # 点击去这里
        self.clickGoHere()
        #切换窗口
        self.switchWindow(-1)
        # 返回网页title作断言
        time.sleep(1)
        asserts = self.getText()
        # 关闭所有浏览器
        # self.driver.quit()

        return asserts

    # 抢租客-主页-搜索地址
    def searchParking(self, address, picture):
        # 切换表单
        self.switchFrame()
        # 输入搜索信息并点击第一个地址
        self.inputSearch(address)

        time.sleep(2)
        if self.imageObj.assertion(picture):
            # print('车位分布图像匹配，测试通过')
            asserts = '车位分布图像匹配，测试通过'
            print(asserts)
        else:
            # print('车位分布图像不匹配，测试不通过')
            asserts = '车位分布图像不匹配，测试不通过'
            print(asserts)
        # self.driver.quit()

        return asserts

    # 抢租客-主页-点击【查看详情】-点击关闭-点击抢购-点击下单
    def getOrder(self):
        # 切换表单
        self.switchFrame()
        # 点击停车位小红点
        self.clickParking()
        # 点击查看详情
        time.sleep(1)
        self.clickDetails()
        # 点击查看详情页的关闭
        self.clickClose()
        # 点击抢购
        self.clickPurchase()
        #点击下单
        self.clickOrder()

        time.sleep(4)
        asserts = self.getOrderText()
        # print(asserts)
        # self.driver.quit()

        return asserts

if __name__ == '__main__':
    # HomeBusiness().goHere()
    HomeBusiness().searchParking('第五国际', 'DWGJ.png')
    # HomeBusiness().getOrder()
